package com.example.neusoft.yuejian_12.Activity;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.Adapter.PhotoInfoAdapter;
import com.example.neusoft.yuejian_12.JavaBean.Photo;
import com.example.neusoft.yuejian_12.Listener.PhotoCommentListener;
import com.example.neusoft.yuejian_12.Listener.PhotoInfoListener;
import com.example.neusoft.yuejian_12.Model.PhotoModel;
import com.example.neusoft.yuejian_12.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PhotoInfoActivity extends AppCompatActivity {
    private List<Photo.CommentEntity> Data;
    private Photo info;
    private RecyclerView recyclerView;
    private String id, userid, reply_userid, thumb, photo_user_pic, photo_user_name, photo_time, photo_title, photo_comment;
    private TextView title, user_name, time ;
    private ImageView pic, userpic;
    private ImageButton back;
    private EditText comment;
    private Button commentbutton;
    private Context mContext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_info);
        final Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id");
        reply_userid = getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("user_id", String.valueOf(0));
        Log.v("获取用户id", String.valueOf(reply_userid));
        PhotoInfo();
        initViews();
        initEvents();
    }

    public void initViews() {
        back = (ImageButton) findViewById(R.id.photo_info_back);
        commentbutton = (Button) findViewById(R.id.photo_info_comment_button);
        title = (TextView) findViewById(R.id.photo_info_intent);
        pic = (ImageView) findViewById(R.id.photo_info_pic);
        user_name = (TextView) findViewById(R.id.photo_info_username);
        userpic = (ImageView) findViewById(R.id.photo_info_userpic);
        time = (TextView) findViewById(R.id.photo_info_time);
        recyclerView = (RecyclerView) findViewById(R.id.rv_comment);
        comment = (EditText) findViewById(R.id.photo_info_comment_intent);
    }

    public void initEvents() {
        recyclerView.setLayoutManager(new LinearLayoutManager(PhotoInfoActivity.this));
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        commentbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photo_comment = comment.getText().toString();
                if (photo_comment.equals("")) {
                    Toast.makeText(PhotoInfoActivity.this, "您输入的信息为空", Toast.LENGTH_LONG).show();
                } else {
                    PhotoModel model=new PhotoModel();
                    model.insertComment(id, userid, reply_userid, photo_comment,PhotoCommentListener);
                    reFresh();
                }
            }
        });
    }

    PhotoCommentListener PhotoCommentListener = new PhotoCommentListener() {
        @Override
        public void onResponse(Integer listBean) {
        }

        @Override
        public void onFail(String msg) {
        }
    };

    //晒图信息包括评论列表
    private void PhotoInfo() {
        PhotoModel model=new PhotoModel();
        model.getPhotoByID(id,PhotoInfoListener);
    }

    PhotoInfoListener PhotoInfoListener = new PhotoInfoListener() {
        @Override
        public void onResponse(Photo listBean) {
            info = listBean;
            Data = info.getComment();
            recyclerView.setAdapter(new PhotoInfoAdapter(PhotoInfoActivity.this,Data));
            initData();
        }

        @Override
        public void onFail(String msg) {
        }
    };

    public void initData() {
        userid = info.getUserid();
        photo_title = info.getTitle();
        photo_user_name =info.getShaier().getUser_name();
        photo_user_pic = info.getShaier().getPhoto();
        photo_time=info.getUpdate_time();
        thumb=info.getThumb0();

        title.setText(photo_title);
        user_name.setText(photo_user_name);
        if (photo_user_pic != ""){
            Picasso.with(PhotoInfoActivity.this)
                    .load("http://172.24.10.175/workout/Uploads/"+photo_user_pic)
                    .into(userpic);
            userpic.setScaleType(ImageView.ScaleType.FIT_XY);
        }else{
            userpic.setImageResource(R.drawable.qya);
        }
        time.setText(photo_time);

        if (thumb != ""){
            Picasso.with(PhotoInfoActivity.this)
                    .load("http://172.24.10.175/workout/Uploads/"+thumb)
                    .into(pic);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
        }else{
            pic.setImageResource(R.drawable.qya);
        }
    }
    private void reFresh(){
        PhotoInfo();
    }
}
